package com.bsoft.gol.hcb.service.templateSheet.service.impl;

import com.bsoft.gol.hcb.service.templateSheet.entity.TemplateSheet;
import com.bsoft.gol.hcb.service.templateSheet.processor.sheetParamProcessor.SheetParamProcessor;
import com.bsoft.gol.hcb.service.templateSheet.processor.transProcessor.TransProcessor;
import com.bsoft.gol.hcb.service.templateSheet.processor.transProcessor.impl.BsXmlTransProcessor;
import com.bsoft.gol.hcb.service.templateSheet.processor.transProcessor.impl.LocalTransProcessor;
import com.bsoft.gol.hcb.service.templateSheet.service.TemplateSheetService;
import ctd.controller.exception.ControllerException;
import ctd.spring.boot.annotation.SsdevService;
import ctd.util.annotation.RpcService;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
 * 基于模版配置的 表单实现
 *
 * @author jinm
 */
@SsdevService("templateSheetService")
public class TemplateSheetServiceImpl implements TemplateSheetService, InitializingBean {

    @Autowired
    @Qualifier("localTransProcessor")
    LocalTransProcessor localTransProcessor;
    @Autowired
    @Qualifier("bsXmlTransProcessor")
    BsXmlTransProcessor bsXmlTransProcessor;
    //自定义参数转换器
    private Map<String, SheetParamProcessor> sheetParamProcessorMap;

    //数据传输处理器
    private Map<String, TransProcessor> transProcessorMap;

    @Override
    public void afterPropertiesSet() throws Exception {
        transProcessorMap = new HashMap<>();
        transProcessorMap.put("1", localTransProcessor);
        transProcessorMap.put("2", bsXmlTransProcessor);
    }

    /**
     * 保存表单
     */
    @RpcService
    @Override
    public TemplateSheet saveTemplateSheet(TemplateSheet templateSheet) throws ControllerException {
        if (templateSheet.getTemplateId() == null ||
            templateSheet.getTransType() == null ||
            CollectionUtils.isEmpty(templateSheet.getSheetParam())) {
            throw new ControllerException("缺少必要参数");
        }
        //根据表单模版id 获取自定义参数转换器 执行自定义转换逻辑
        SheetParamProcessor sheetParamProcessor = getProcessor(sheetParamProcessorMap, templateSheet.getTemplateId().toString());
        if (sheetParamProcessor != null) {
            sheetParamProcessor.outConvert(templateSheet.getSheetParam());
        }

        //根据TransType 找到对应的传输处理器  并执行
        TransProcessor transProcessor = getProcessor(transProcessorMap, templateSheet.getTransType().toString());
        if (transProcessor == null) {
            throw new ControllerException("未找到表单处理器");
        }

        try {
            return transProcessor.outTrans(templateSheet);
        } catch (Exception e) {
            throw new ControllerException(e.getMessage());
        }
    }

    /**
     * 查询表单详情
     */
    @RpcService
    @Override
    public TemplateSheet getTemplateSheet(TemplateSheet templateSheet) throws ControllerException {
        if (templateSheet.getTemplateId() == null ||
            templateSheet.getTransType() == null ||
            StringUtils.isEmpty(templateSheet.getRecordId())) {
            throw new ControllerException("缺少必要参数");
        }

        //根据TransType 找到对应的传输处理器  并执行
        TransProcessor transProcessor = getProcessor(transProcessorMap, templateSheet.getTransType().toString());
        if (transProcessor == null) {
            throw new ControllerException("未找到表单处理器");
        }

        try {
            transProcessor.inTrans(templateSheet);
        } catch (Exception e) {
            throw new ControllerException(e.getMessage());
        }

        //根据表单模版id 获取自定义参数转换器 执行自定义转换逻辑
        SheetParamProcessor sheetParamProcessor = getProcessor(sheetParamProcessorMap, templateSheet.getTemplateId().toString());
        if (sheetParamProcessor != null) {
            sheetParamProcessor.inConvert(templateSheet.getSheetParam());
        }

        return templateSheet;
    }

    public Map<String, SheetParamProcessor> getSheetParamProcessorMap() {
        return sheetParamProcessorMap;
    }

    public void setSheetParamProcessorMap(Map<String, SheetParamProcessor> sheetParamProcessorMap) {
        this.sheetParamProcessorMap = sheetParamProcessorMap;
    }

    public Map<String, TransProcessor> getTransProcessorMap() {
        return transProcessorMap;
    }

    public void setTransProcessorMap(Map<String, TransProcessor> transProcessorMap) {
        this.transProcessorMap = transProcessorMap;
    }

    private <T> T getProcessor(Map<String, T> processorMap, String code) {
        if (CollectionUtils.isEmpty(processorMap)) {
            return null;
        }
        return processorMap.get(code);
    }


}
